package com.hns.can.service.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.hns.can.common.entity.BaseEntity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.service.entity.BaseStnBevParamInfoEntity;
import com.hns.can.service.entity.BaseStnDuraParamInfoEntity;
import com.hns.can.service.entity.BsDayStnBevDetailEntity;
import com.hns.can.service.entity.BsDayStnDuraDetailEntity;
import com.hns.can.service.service.OperationReportService;
import com.hns.can.service.vo.LineParameterVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;

/**
 * 线路参数设置
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/operationReport")
public class OperationReportController extends BaseController {
    
    @Autowired
    private OperationReportService operationReportService;  
    @Autowired
    private CommonInitParamService commonInitParamService;
    
    /**
     * 导出文件表头
     */
    private final String[] tableHeaderNames={"驾驶员","车牌号","车辆编号","运营线路","所属机构","发生时间","方向","站点","类别","进站时间","出站时间","车速是否出现0 0-否 1-是","车门是否打开0 0-否 1-是","时长(s)","参数值(s)"};
    private final String[] tableHeaderNames2={"驾驶员","车牌号","运营线路","所属机构","发生时间","方向","开始站点","结束站点","开始时间","结束时间","时长(s)","参数值(s)"};
    /**
     * 导出文件字段名
     */
    private final String[] tableHeaderFields={"drvName", "licPltNo", "carInCd" , "lineName", "organName", "rcrdTime", "lineType", "stnName","cgy","getinTime","getoutTime","wthrSpdOfMotVhi","wthrCar","dura","paramSetValue"};
    private final String[] tableHeaderFields2={"drvName", "licPltNo", "lineName", "organName", "rcrdTime", "lineType", "beginStnName","endStnName","beginTime","endTime","oprtDura","paramSet"};
    

    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-18
     * @功能说明：获取站点行为版本
     */
    @RequestMapping("/getVrsnName")
    @ResponseBody
    public Object getVrsnName (LineParameterVo vo) throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCurrentTime(DateTool.getCurrentSysStandDate());
        vo.setCorpId(user.getCorpId());
        List<BaseStnBevParamInfoEntity> list = operationReportService.getVrsnName(vo);
        return this.getReturnResult(list);
    }

    /**
     *
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-19
     * @功能说明：查询线路信息
     */
    @RequestMapping("/searchStationBehaviorPage")
    @ResponseBody
    public Object searchStationBehaviorPage(LineParameterVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List<LineParameterVo> list;
        if("edit".equals(vo.getAddOrEdit())){
            list = operationReportService.searchlineParameterInfo(vo);
        }else{
            list = operationReportService.searchStationInfo(vo);
        }
        return this.getReturnResult(list);
    }
    
    /**
     * 
     * @param vo
     * @throws BusinessException
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-25
     * @功能说明：检验
     */
    @RequestMapping("/checkVersion")
    @ResponseBody
    public Object checkVersion(LineParameterVo vo) throws Exception {
        int count= operationReportService.checkVersion(vo);
        if(count > 0){
            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的版本已存在");
        }
        count= operationReportService.checkByData(vo);
        if(count > 0){
            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的时间与日期类型有冲突(相同日期类型所输时间不能有交集)");
        };
        //保存
        operationReportService.saveStationBehavior(vo);
        return this.getReturnResult("保存成功");
    }
    
    /**
     * 
     * @throws BusinessException
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-6-27
     * @功能说明： 保存
     */
    @RequestMapping("/saveStationBehavior")
    @ResponseBody
    public Object saveStationBehavior(LineParameterVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCrtUser(user.getAcctName());
        vo.setUpdateUser(user.getAcctName());
        int count= operationReportService.checkByData(vo);
        if(count > 0){
            throw new BusinessException(MsgType.WARN.getFlag(), "不同版本，相同启用时间、相同日期类型（工作日、周末、通用）时，您输入的时间不能有交集");
        }
        operationReportService.saveStationBehavior(vo);
        return this.getReturnResult("保存成功");
    }
    
    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-1
     * @功能说明：删除
     */
    @RequestMapping("/delStationBehavior")
    @ResponseBody
    public Object delStationBehavior(LineParameterVo vo) throws Exception {
        operationReportService.delStationBehavior(vo);
        return this.getReturnResult("删除成功");
    }
    
    /**
     * 
     * @param entity
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-18
     * @功能说明：获取站间时长版本
     */
    @RequestMapping("/getStnDuraVrsnName")
    @ResponseBody
    public Object getStnDuraVrsnName (BaseStnDuraParamInfoEntity entity) throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCorpId(user.getCorpId());
        List<BaseStnBevParamInfoEntity> list = operationReportService.getStnDuraVrsnName(entity);
        return this.getReturnResult(list);
    }
    
    /**
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-19
     * @功能说明：查询站间时长线路信息
     */
    @RequestMapping("/searchStnDura")
    @ResponseBody
    public Object searchStnDura(LineParameterVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List<LineParameterVo> list = null;
        list = operationReportService.searchStnDuraInfo(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 
     * @param vo
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-6
     * @功能说明： 新增
     */
    @RequestMapping("/addStnDura")
    public void addStnDura(LineParameterVo vo) throws Exception {
        checkStnDura(vo);
    }
    
    /**
     * 
     * @param vo
     * @throws BusinessException
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-6-25
     * @功能说明：站间时长检验
     */
    @RequestMapping("/checkStnDura")
    @ResponseBody
    public Object checkStnDura(LineParameterVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCrtUser(user.getName());
        vo.setUpdateUser(user.getName());
        int count= operationReportService.checkStnDura(vo);
//        if(count > 0){
//            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的版本已存在");
//        }
        count= operationReportService.checkStnDuraByData(vo);
//        if(count > 0){
//            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的时间与日期类型有冲突(相同日期类型所输时间不能有交集)");
//        }
        //保存
        operationReportService.saveStnDura(vo);
        return this.getReturnResult("保存成功");
    }
    
    /**
     * 
     * @throws BusinessException
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-6-27
     * @功能说明： 保存——站间时长
     */
    @RequestMapping("/saveStnDura")
    @ResponseBody
    public Object saveStnDura(LineParameterVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCrtUser(user.getAcctName());
        vo.setUpdateUser(user.getAcctName());
//        int count= operationReportService.checkStnDuraByData(vo);
//        if(count > 0){
//            throw new BusinessException(MsgType.WARN.getFlag(), "不同版本，相同启用时间、相同日期类型（工作日、周末、通用）时，您输入的时间不能有交集");
//        }
        operationReportService.saveStnDura(vo);
        return this.getReturnResult("保存成功");
    }
    
    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-1    
     * @功能说明：删除——站间时长
     */
    @RequestMapping("/delStnDura")
    @ResponseBody
    public Object delStnDura(LineParameterVo vo) throws Exception {
        String ids=vo.getVrsnCd().substring(0,vo.getVrsnCd().length()-1);
        String[] a=ids.split(",");
        operationReportService.delStnDura(a);
        return this.getReturnResult("删除成功");
    }
    
    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015-7-9
     * @功能说明： 查询滞站越站监控
     */
     @RequestMapping("/searchStationBehaviorMonitor")
     @ResponseBody
     public Object searchStationBehaviorMonitor(LineParameterVo entity) throws Exception {
         SysUserEntity user = this.getCurrentUser().getUser();
         entity.setCorpId(user.getCorpId());
         commonInitParamService.initParamsLimitCar(entity,user, Arrays.asList("T_BS_DAY_STN_BEV_DETAIL"));
         Pagination page = operationReportService.searchStationBehaviorMonitor(entity);
         GridPage grid = new GridPage(page);
         return this.getReturnResult(grid);
     }
    
    
     /**
      * 导出滞站越站监控excel
      * @param entity
      * @param response
      * @throws Exception
      * @author:lan
      * @email:763836737@qq.com
      */
     @RequestMapping("/exportStationBehaviorMonitorExcel")
     public void exportStationBehaviorMonitorExcel(LineParameterVo entity, HttpServletResponse response) throws Exception {
         SysUserEntity user = this.getCurrentUser().getUser();
         entity.setCorpId(user.getCorpId());
         List<BaseEntity> list = operationReportService.getStationBehaviorMonitorList(entity);
         ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", URLEncoder.encode("滞站越站明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "滞站越站监控");
     }
     
     
     /**
      * 导出滞站越站监控pdf
      * @param entity
      * @param response
      * @throws Exception
      * @author:lan
      * @email:763836737@qq.com
      */
     @RequestMapping("/exportStationBehaviorMonitorPdf")
     @ResponseBody
     public void exportStationBehaviorMonitorPdf(LineParameterVo entity, HttpServletResponse response) throws Exception {
         SysUserEntity user = this.getCurrentUser().getUser();
         entity.setCorpId(user.getCorpId());
         List<BaseEntity> list = operationReportService.getStationBehaviorMonitorList(entity);
         ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", URLEncoder.encode("滞站越站明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "滞站越站监控");
     }
     
     
    /**
     * 
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2015年7月9日
     * @功能说明： 查询站间时长监控
     */
    @RequestMapping("/searchStnDuraMonitor")
    @ResponseBody
    public Object searchStnDuraMonitor(LineParameterVo entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCorpId(user.getCorpId());
        commonInitParamService.initParamsLimitCar(entity,user, Arrays.asList("T_BS_DAY_STN_DURA_DETAIL"));
        Pagination page = operationReportService.searchStnDuraMonitor(entity);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    
    /**
     * 导出站间时长监控excel
     * @param entity
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStnDuraMonitorExcel")
    public void exportStnDuraMonitorExcel(BsDayStnDuraDetailEntity entity, HttpServletResponse response) throws Exception {
        entity.setUserId(this.getCurrentUser().getUser().getId());
        List<BsDayStnDuraDetailEntity> list = operationReportService.getStnDuraMonitorList(entity);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames2), Arrays.asList(tableHeaderFields2), list, response, "excel", URLEncoder.encode("站间时长明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "站间时长监控");
    }
    
    
    /**
     * 导出站间时长监pdf
     * @param entity
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportStnDuraMonitorPdf")
    public void exportStnDuraMonitorPdf(BsDayStnDuraDetailEntity entity, HttpServletResponse response) throws Exception {
        entity.setUserId(this.getCurrentUser().getUser().getId());
        List<BsDayStnDuraDetailEntity> list = operationReportService.getStnDuraMonitorList(entity);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames2), Arrays.asList(tableHeaderFields2), list, response, "pdf", URLEncoder.encode("站间时长明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "站间时长监");
    }
    
}
